// pages/home/home.js
const httpUtils = require('../../../utils/httpUtils')
const ui = require('../../../utils/ui')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 0,
    autoplay: true,
    duration: 500,
    interval: 5000,
    swiperList: [],
    recordDayNum: 0,
    myRecordNum: 0,
    shareMeNum: 0,
    border: {
      color: '#f6f6f6',
    },
    showMultiTextAndTitle: false,
    confirmBtn: {
      content: '知道了',
      variant: 'base'
    },
    notice: {
      content: ''
    }
  },
  //关闭公告弹窗
  closeDialog: function () {
    let id = this.data.notice.id;
    let obj = {
      method: 'GET',
      showLoading: false,
      url: 'notice/readNotice?id=' + id
    }
    httpUtils.request(obj).then(res => {
      if (res.data.retCode == 0) {
        this.setData({
          notice: {
            content: ''
          },
          showMultiTextAndTitle: false
        })
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },
  //查询系统公告
  getNoticeInfo: function () {
    let obj = {
      method: 'GET',
      showLoading: false,
      url: 'notice/getMyNotice'
    }
    httpUtils.request(obj).then(res => {
      if (res.data.retCode == 0) {
        if (null != res.data.data) {
          this.setData({
            notice: res.data.data,
            showMultiTextAndTitle: true
          })
        }
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },
  onTap: function (e) {
    const {
      index
    } = e.detail;
    console.log(index);
  },
  onChange: function (e) {
    const {
      current,
      source
    } = e.detail;
    console.log(current, source);
  },
  //查询轮播图集合
  getSwiperList: function () {
    let obj = {
      method: 'GET',
      showLoading: false,
      url: 'swiper/getSwiperList'
    }
    httpUtils.request(obj).then(res => {
      if (res.data.retCode == 0) {
        this.setData({
          swiperList: res.data.data
        })
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },

  //查询日记相关统计
  getNotebookCount: function () {
    let obj = {
      method: 'GET',
      showLoading: false,
      url: 'home/countNotebook'
    }
    httpUtils.request(obj).then(res => {
      if (res.data.retCode == 0) {
        this.setData({
          recordDayNum: res.data.data.recordDayNum,
          myRecordNum: res.data.data.myRecordNum,
          shareMeNum: res.data.data.shareMeNum,
        })
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getNoticeInfo();
    this.getSwiperList();
    this.getNotebookCount();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})